package com.bjbn.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理器
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理通用异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleException(Exception e) {
        log.error("发生未预期的异常", e);
        Map<String, Object> response = new HashMap<>();
        response.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
        response.put("message", "系统内部错误: " + e.getMessage());
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理HTTP请求异常
     */
    @ExceptionHandler(org.springframework.web.client.HttpClientErrorException.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleHttpClientErrorException(org.springframework.web.client.HttpClientErrorException e) {
        log.error("HTTP客户端请求异常", e);
        Map<String, Object> response = new HashMap<>();
        response.put("code", e.getStatusCode().value());
        response.put("message", "HTTP请求错误: " + e.getStatusText());
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, e.getStatusCode());
    }

    /**
     * 处理数据库相关异常
     */
    @ExceptionHandler(org.springframework.jdbc.BadSqlGrammarException.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleBadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException e) {
        log.error("数据库SQL语法异常", e);
        Map<String, Object> response = new HashMap<>();
        response.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
        response.put("message", "数据库操作错误: SQL语法不正确");
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理超时异常
     */
    @ExceptionHandler(java.util.concurrent.TimeoutException.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleTimeoutException(java.util.concurrent.TimeoutException e) {
        log.error("操作超时异常", e);
        Map<String, Object> response = new HashMap<>();
        response.put("code", HttpStatus.REQUEST_TIMEOUT.value());
        response.put("message", "操作超时，请稍后重试");
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.REQUEST_TIMEOUT);
    }

    /**
     * 处理空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleNullPointerException(NullPointerException e) {
        log.error("空指针异常", e);
        Map<String, Object> response = new HashMap<>();
        response.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
        response.put("message", "系统内部错误: 空指针异常");
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理自定义业务异常
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleBusinessException(BusinessException e) {
        log.warn("业务异常: {}", e.getMessage());
        Map<String, Object> response = new HashMap<>();
        response.put("code", e.getCode());
        response.put("message", e.getMessage());
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }
}